package com.swd.order.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.charset.Charset;
import java.util.Map;

@Slf4j
public class HttpUtil {

//    public static HttpEntity getRequest(String requestUrl, Map<String,String> map) throws Exception {
//
//        CloseableHttpClient httpClient = HttpClients.createDefault();
//        URIBuilder uriBuilder = new URIBuilder(requestUrl);
//        map.forEach(uriBuilder::addParameter);
//        HttpGet httpGet = new HttpGet(uriBuilder.build());
//        return  httpClient.execute(httpGet).getEntity();
//
//    }

    public static HttpEntity getRequest(String requestUrl, Map<String,String> urlMap) throws Exception {

        CloseableHttpClient httpClient = HttpClients.createDefault();
        URIBuilder uriBuilder = new URIBuilder(requestUrl);
        urlMap.forEach(uriBuilder::addParameter);
        HttpGet httpGet = new HttpGet(uriBuilder.build());
        return  httpClient.execute(httpGet).getEntity();

    }


    public static HttpEntity postRequest(String requestUrl, Map<String,String> map) throws IOException {
        CloseableHttpClient httpClient = HttpClients.createDefault();

        MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();

        map.forEach((key,value)->{
            multipartEntityBuilder.addTextBody(key, value, ContentType.DEFAULT_TEXT);
        });

        HttpPost httpPost = new HttpPost(requestUrl);
        httpPost.setEntity(multipartEntityBuilder.build());
        return  httpClient.execute(httpPost).getEntity();
    }

    public static HttpEntity postRequest(String requestUrl,Map<String,String> urlMap,Map<String,String> map) throws Exception, URISyntaxException {
        CloseableHttpClient httpClient = HttpClients.createDefault();

        MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();

        map.forEach((key,value)->{
            multipartEntityBuilder.addTextBody(key, value, ContentType.DEFAULT_TEXT);
        });

        URIBuilder uriBuilder = new URIBuilder(requestUrl);
        urlMap.forEach(uriBuilder::addParameter);

        HttpPost httpPost = new HttpPost(uriBuilder.build());
        httpPost.setEntity(multipartEntityBuilder.build());
        return  httpClient.execute(httpPost).getEntity();
    }

    public static HttpEntity postJsonRequest(String requestUrl,Map<String,String> urlMap,String json) throws Exception, URISyntaxException {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        StringEntity stringEntity = new StringEntity(json,"UTF-8");
        stringEntity.setContentType("application/json");

        URIBuilder uriBuilder = new URIBuilder(requestUrl);
        urlMap.forEach(uriBuilder::addParameter);
        HttpPost httpPost = new HttpPost(uriBuilder.build());
        httpPost.addHeader("Content-type","application/json; charset=utf-8");
        httpPost.setHeader("Accept", "application/json");
        httpPost.setEntity(stringEntity);
        log.info(httpPost.getURI().toString());
        return  httpClient.execute(httpPost).getEntity();
    }


}
